package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.pojo.Bill;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface BillDao {
    /**
     * 查看转出方为accountId的流水
     */
    @Results(id = "billResultMap", value = {
            @Result(column = "bill_id", property = "billId", typeHandler = IdTypeHandler.class),
            @Result(column = "send_account_id", property = "sendAccountId", typeHandler = IdTypeHandler.class),
            @Result(column = "receive_account_id", property = "receiveAccountId", typeHandler = IdTypeHandler.class)
    })
    @Select("select * from t_bill where send_account_id = #{accountId}")
    List<Bill> getSendBillByAccountId(String accountId);

    /**
     * 转入的流水
     */
    @ResultMap("billResultMap")
    @Select("select * from t_bill where receive_account_id = #{accountId}")
    List<Bill> getReceiveBillByAccountId(String accountId);

    /**
     * 全部流水
     */
    @ResultMap("billResultMap")
    @Select("select * from t_bill where " +
            "receive_account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "or send_account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    List<Bill> getBillByAccountId(String accountId);

    @ResultMap("billResultMap")
    @Select("select * from t_bill where " +
            "send_account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "and bill_time > #{startTime} " +
            "and bill_time < #{endTime}")
    List<Bill> getSendBillByAccountIdTime(String accountId, Date startTime, Date endTime);

    /**
     * 转入的流水
     */
    @ResultMap("billResultMap")
    @Select("select * from t_bill where " +
            "receive_account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "and bill_time > #{startTime} " +
            "and bill_time < #{endTime}")
    List<Bill> getReceiveBillByAccountIdTime(String accountId, Date startTime, Date endTime);

    /**
     * 全部流水
     */
    @ResultMap("billResultMap")
    @Select("select * from t_bill where " +
            "(receive_account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "or send_account_id = #{accountId, typeHandler=com.fudan.backend.handler.IdTypeHandler}) " +
            "and bill_time > #{startTime} " +
            "and bill_time < #{endTime}")
    List<Bill> getBillByAccountIdTime(String accountId, Date startTime, Date endTime);
    @Insert("insert into t_bill values " +
            "(#{billId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{sendAccountId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{receiveAccountId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{billAmount}, " +
            "#{billTime}, " +
            "#{billType}, " +
            "#{moreInfo})")
    Integer addBill(Bill bill);
}
